// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.znet;

import java.util.ArrayList;
import java.util.List;
import se.krka.kahlua.vm.KahluaTable;
import zombie.Lua.LuaEventManager;
import zombie.Lua.LuaManager;
import zombie.network.Server;

public class ServerBrowser {
    private static IServerBrowserCallback m_callbackInterface = null;

    public static boolean init() {
        boolean _boolean = false;
        if (SteamUtils.isSteamModeEnabled()) {
            _boolean = n_Init();
        }

        return _boolean;
    }

    public static void shutdown() {
        if (SteamUtils.isSteamModeEnabled()) {
            n_Shutdown();
        }
    }

    public static void RefreshInternetServers() {
        if (SteamUtils.isSteamModeEnabled()) {
            n_RefreshInternetServers();
        }
    }

    public static int GetServerCount() {
        int _int = 0;
        if (SteamUtils.isSteamModeEnabled()) {
            _int = n_GetServerCount();
        }

        return _int;
    }

    public static GameServerDetails GetServerDetails(int serverIndex) {
        GameServerDetails gameServerDetails = null;
        if (SteamUtils.isSteamModeEnabled()) {
            gameServerDetails = n_GetServerDetails(serverIndex);
        }

        return gameServerDetails;
    }

    public static void Release() {
        if (SteamUtils.isSteamModeEnabled()) {
            n_Release();
        }
    }

    public static boolean IsRefreshing() {
        boolean _boolean = false;
        if (SteamUtils.isSteamModeEnabled()) {
            _boolean = n_IsRefreshing();
        }

        return _boolean;
    }

    public static boolean QueryServer(String host, int port) {
        boolean _boolean = false;
        if (SteamUtils.isSteamModeEnabled()) {
            _boolean = n_QueryServer(host, port);
        }

        return _boolean;
    }

    public static GameServerDetails GetServerDetails(String host, int port) {
        GameServerDetails gameServerDetails = null;
        if (SteamUtils.isSteamModeEnabled()) {
            gameServerDetails = n_GetServerDetails(host, port);
        }

        return gameServerDetails;
    }

    public static void ReleaseServerQuery(String host, int port) {
        if (SteamUtils.isSteamModeEnabled()) {
            n_ReleaseServerQuery(host, port);
        }
    }

    public static List<GameServerDetails> GetServerList() {
        ArrayList arrayList = new ArrayList();
        if (SteamUtils.isSteamModeEnabled()) {
            try {
                while (IsRefreshing()) {
                    Thread.sleep(100L);
                    SteamUtils.runLoop();
                }
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }

            for (int _int = 0; _int < GetServerCount(); _int++) {
                GameServerDetails gameServerDetails = GetServerDetails(_int);
                if (gameServerDetails.steamId != 0L) {
                    arrayList.add(gameServerDetails);
                }
            }
        }

        return arrayList;
    }

    public static GameServerDetails GetServerDetailsSync(String host, int port) {
        GameServerDetails gameServerDetails = null;
        if (SteamUtils.isSteamModeEnabled()) {
            gameServerDetails = GetServerDetails(host, port);
            if (gameServerDetails == null) {
                QueryServer(host, port);

                try {
                    while (gameServerDetails == null) {
                        Thread.sleep(100L);
                        SteamUtils.runLoop();
                        gameServerDetails = GetServerDetails(host, port);
                    }
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }
        }

        return gameServerDetails;
    }

    public static boolean RequestServerRules(String host, int port) {
        return n_RequestServerRules(host, port);
    }

    public static void setCallbackInterface(IServerBrowserCallback callbackInterface) {
        m_callbackInterface = callbackInterface;
    }

    private static native boolean n_Init();

    private static native void n_Shutdown();

    private static native void n_RefreshInternetServers();

    private static native int n_GetServerCount();

    private static native GameServerDetails n_GetServerDetails(int var0);

    private static native void n_Release();

    private static native boolean n_IsRefreshing();

    private static native boolean n_QueryServer(String var0, int var1);

    private static native GameServerDetails n_GetServerDetails(String var0, int var1);

    private static native void n_ReleaseServerQuery(String var0, int var1);

    private static native boolean n_RequestServerRules(String var0, int var1);

    private static void onServerRespondedCallback(int _int) {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnServerResponded(_int);
        }

        LuaEventManager.triggerEvent("OnSteamServerResponded", _int);
    }

    private static void onServerFailedToRespondCallback(int _int) {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnServerFailedToRespond(_int);
        }
    }

    private static void onRefreshCompleteCallback() {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnRefreshComplete();
        }

        LuaEventManager.triggerEvent("OnSteamRefreshInternetServers");
    }

    private static void onServerRespondedCallback(String string, int _int) {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnServerResponded(string, _int);
        }

        GameServerDetails gameServerDetails = GetServerDetails(string, _int);
        if (gameServerDetails != null) {
            Server server = new Server();
            server.setName(gameServerDetails.name);
            server.setDescription("");
            server.setSteamId(Long.toString(gameServerDetails.steamId));
            server.setPing(Integer.toString(gameServerDetails.ping));
            server.setPlayers(Integer.toString(gameServerDetails.numPlayers));
            server.setMaxPlayers(Integer.toString(gameServerDetails.maxPlayers));
            server.setOpen(true);
            if (gameServerDetails.tags.contains("hidden")) {
                server.setOpen(false);
            }

            server.setIp(gameServerDetails.address);
            server.setPort(Integer.toString(gameServerDetails.port));
            server.setMods("");
            if (!gameServerDetails.tags.replace("hidden", "").replace("hosted", "").replace(";", "").isEmpty()) {
                server.setMods(gameServerDetails.tags.replace(";hosted", "").replace("hidden", ""));
            }

            server.setHosted(gameServerDetails.tags.endsWith(";hosted"));
            server.setVersion("");
            server.setLastUpdate(1);
            server.setPasswordProtected(gameServerDetails.passwordProtected);
            ReleaseServerQuery(string, _int);
            LuaEventManager.triggerEvent("OnSteamServerResponded2", string, (double)_int, server);
        }
    }

    private static void onServerFailedToRespondCallback(String string, int _int) {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnServerFailedToRespond(string, _int);
        }

        LuaEventManager.triggerEvent("OnSteamServerFailedToRespond2", string, (double)_int);
    }

    private static void onRulesRefreshComplete(String string0, int _int, String[] string1) {
        if (m_callbackInterface != null) {
            m_callbackInterface.OnSteamRulesRefreshComplete(string0, _int);
        }

        KahluaTable kahluaTable = LuaManager.platform.newTable();

        for (byte _byte = 0; _byte < string1.length; _byte += 2) {
            kahluaTable.rawset(string1[_byte], string1[_byte + 1]);
        }

        LuaEventManager.triggerEvent("OnSteamRulesRefreshComplete", string0, (double)_int, kahluaTable);
    }
}
